﻿Imports System.Data.OleDb

Public Class AddNewScene
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btn_addScene_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_addScene.Click

        Dim newFileName As String = ""
        If f_sceneImg.HasFile Then
            Dim myGUID = Guid.NewGuid()
            Dim fileExtension As String
            fileExtension = System.IO.Path.GetExtension(f_sceneImg.FileName).ToLower()
            If fileExtension = ".jpg" Then
                newFileName = myGUID.ToString() & ".jpg"
            ElseIf fileExtension = ".png" Then
                newFileName = myGUID.ToString() & ".png"
            ElseIf fileExtension = ".gif" Then
                newFileName = myGUID.ToString() & ".gif"
            End If
            Dim fileLocationOnServerHardDisk = Request.MapPath("~/Pictures") & "/" & newFileName
            f_sceneImg.SaveAs(fileLocationOnServerHardDisk)
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("TwilightConn").ConnectionString)
        Dim SqlString As String = "INSERT INTO SceneList(SceneName,SceneImg,KeyCharacters,KeyBook,SceneDescription,ExtraURL,TwiWriter) VALUES (@f1,@f2,@f3,@f4,@f5,@f6,@f7)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)

        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_sceneName.Text)
        If f_sceneImg.HasFile Then
            cmd.Parameters.AddWithValue("@f2", "Pictures/" & newFileName)
        Else
            cmd.Parameters.AddWithValue("@f2", "Pictures/Design/defaultScene.jpg")
        End If
        cmd.Parameters.AddWithValue("@f3", tb_sceneChars.Text)
        cmd.Parameters.AddWithValue("@f4", ddl_keybook.SelectedValue)
        cmd.Parameters.AddWithValue("@f5", tb_sceneDesc.Text)
        If tb_sceneURL.Text.Length > 0 Then
            cmd.Parameters.AddWithValue("@f6", tb_sceneURL.Text)
        Else
            cmd.Parameters.AddWithValue("@f6", "http://www.stepheniemeyer.com/twilightseries.html")
        End If
        cmd.Parameters.AddWithValue("@f7", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()
        Response.Redirect("NewSceneConfirmation.aspx")
    End Sub
End Class